package com.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.po.User;
import com.service.UserService;

@Controller
public class UserAction {
	@Resource
	private UserService userService;
	@RequestMapping(value="/reg.do",method=RequestMethod.POST)
	public String userreg(User user){
		System.out.println(user.getUsername());
		userService.SaveUser(user);
		return "redirect:/userlist.do";
	}
	@RequestMapping(value="/reg.do",method=RequestMethod.GET)
	public String showreg(){
		return "reg.jsp";
	}
	@RequestMapping("/userlist.do")
	public String userlist(Model model){
		List<User> list=userService.userlist();
		model.addAttribute("userlist",list);
		return "userlist.jsp";
	}
	@RequestMapping(value="/userapi.do",params="action=edit",method=RequestMethod.POST)
	public String useredit(User user){
		System.out.println("更新用户");
		userService.Update(user);
		return "redirect:/userlist.do";
	}
	@RequestMapping(value="/user.do",params="action=edit")
	public String useredia(int id,Model model){
		model.addAttribute("user", userService.getUserById(id));
		return "edit.jsp";
	}
	@RequestMapping(value="/user.do",params="action=delete")
	public String userdelete(int id){
		userService.DelUser(id);
		return "redirect:/userlist.do";
	}
}
